//
//  AppDelegate.m
//  LubanWork
//
//  Created by xiaohao on 2019/12/31.
//  Copyright © 2019 xiaohao. All rights reserved.
//

#import "AppDelegate.h"
#import "LoginViewController.h"
#import "BBTabBarController.h"
#import "BBNavigationController.h"
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <OpenInstallSDK.h>
#import "LoginViewController.h"
@interface AppDelegate ()<AMapLocationManagerDelegate,OpenInstallDelegate>

@property (nonatomic,strong) AMapLocationManager* mapManager;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if (@available(iOS 13.0, *)) {
    //        UIWindowScene *windowScene = (UIWindowScene *)scene;
            self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //        [self.window setWindowScene:windowScene];
            [self.window setBackgroundColor:[UIColor whiteColor]];
            
            self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
            
        }
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [OpenInstallSDK initWithDelegate:self];
//    [NSThread sleepForTimeInterval:3.0];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [[RealReachability sharedInstance] startNotifier];
    [AMapServices sharedServices].apiKey=@"78ca9e148601c3fb0b1b0bf43bf41272";
    self.mapManager = [[AMapLocationManager alloc] init];
    self.mapManager.delegate = self;
    [self.mapManager setLocatingWithReGeocode:YES];
    [self.mapManager startUpdatingLocation];
//    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
//    UserModel *model = [UserModel sharedInstance:UserKey0];
//    if (model.isLogin) {
        [self setTabbarViewController];
//    }
//    else{
//        [self setLoginViewController];
//    }
    [self.window makeKeyAndVisible];
    
    NSString *endpoint = @"https://oss-cn-hongkong.aliyuncs.com";
    
//    public static final String endpoint = "http://oss-cn-hongkong.aliyuncs.com";
//    public static final String accessKeyId = "LTAI3RU4GC5ulCXd";
//    public static final String accessKeySecret = "UrNBQDbZJCKEDdsQsqUWKqlPIDHeMV";
//    public static final String bucketName = "kuailejituan";//"hgjt-oss";
//    public static final String objectName = "Image";
    
    id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:@"LTAI3RU4GC5ulCXd" secretKeyId:@"UrNBQDbZJCKEDdsQsqUWKqlPIDHeMV" securityToken:@""];
    
    self.client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
    
    return YES;
}
-(void)setLoginViewController{
    BBNavigationController *nav = [[BBNavigationController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
    [self.window setRootViewController:nav];
}

-(void)setTabbarViewController{

    BBTabBarController *tabbar = [[BBTabBarController alloc] init];
    [self.window setRootViewController:tabbar];
}
+ (AppDelegate* )shareAppDelegate {
    return (AppDelegate*)[UIApplication sharedApplication].delegate;
}
- (void)setupRootController {
    UserModel * model=[UserModel sharedInstance:UserKey0];
    if (model.isLogin) {
        [self setTabbarViewController];
    }else{
        [self setLoginViewController];
    }
}
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode{
    if (![UtilTools isBlankString:reGeocode.country]) {
        if (![UtilTools isBlankString:reGeocode.province]) {
            if ([reGeocode.province hasPrefix:@"黑龙江"]) {
                self.province=@"黑龙江";
            }else if ([reGeocode.province hasPrefix:@"内蒙古"]) {
                self.province=@"内蒙古";
            }else{
                self.province=[reGeocode.province substringToIndex:2];
            }
            self.location=self.province;
            [manager stopUpdatingLocation];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"updateProvice" object:nil];
        }
    }
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
    //判断是否通过OpenInstall Universal Link 唤起App
    if ([OpenInstallSDK continueUserActivity:userActivity]){//如果使用了Universal link ，此方法必写
        return YES;
    }
    //其他第三方回调；
     return YES;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(nonnull NSDictionary *)options{
    //判断是否通过OpenInstall URL Scheme 唤起App
    if  ([OpenInstallSDK handLinkURL:url]){//必写
        return YES;
    }
    //其他第三方回调；
     return YES;

}

@end
